// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head: '/icons/user.png',
    username: '未登录',
    phone: ''
  },
  login(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  logout(){
    // 之前登录保存的数据清空
    wx.clearStorageSync()
    this.user = null

    this.setData({
      head: '/icons/user.png',
      username: '未登录',
      phone: ''
    })
    this.onShow()
  },
  uploadUserHead(){
    let user = wx.getStorageSync('user')
        if(!user){
          wx.showModal({
            content: '用户请先登录',
          })
          return
        }
    // 调用小程序sdk 打开相册或相机
    wx.chooseImage({
      count: 1,
      success: res =>{
        const filepath = res.tempFilePaths[0]
        // 上传头像
        wx.uploadFile({
          filePath: filepath,
          name: 'head',
          url: getApp().baseURL + '/user/upload/',
          header: {Authorization: 'Token '+ user.token},
          success: res =>{
            console.info(res.data)
            const {path} = JSON.parse(res.data)
            // 更新本地存储
            user.head = getApp().baseURL + path+'?t='+new Date().getTime()
            wx.setStorageSync('user', user)
            this.setData({
              head:user.head
            })
          }
        })
      }
    })
  },
  indentDetail(){
    let user = wx.getStorageSync('user')
    if(!user){
      wx.showModal({
        content: '用户请先登录',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/indent/indent',
    })
  },
  collectionDetail(){
    let user = wx.getStorageSync('user')
    if(!user){
      wx.showModal({
        content: '用户请先登录',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/collection/collection',
    })
  },
  areaDetail(){
    let user = wx.getStorageSync('user')
    if(!user){
      wx.showModal({
        content: '用户请先登录',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/area/area',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.user = wx.getStorageSync('user')
    if(this.user){
      this.setData({
        phone: this.user.phone,
        username: this.user.username,
        head: this.user.head
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.info('---onHide---')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})